import logging
import os


class Logger(object):
    # 日志级别关系映射
    level_relations = {
        'debug': logging.DEBUG,
        'info': logging.INFO,
        'warning': logging.WARNING,
        'error': logging.ERROR,
        'crit': logging.CRITICAL
    }

    def __init__(self,
                 root_path,
                 log_name,
                 level='info',
                 fmt='%(asctime)s - %(levelname)s: %(message)s'):
        # 日志保存路径
        self.root_path = root_path
        # 初始化logger名称和格式
        self.log_name = log_name
        # 初始格式
        self.fmt = fmt
        # 先声明一个 logger对象
        self.logger = logging.getLogger(log_name)
        # 设置日志级别
        self.logger.setLevel(self.level_relations.get(level))

    def get_logger(self):
        path = os.path.join(self.root_path, 'log')
        print(f'self.root_path {self.root_path}')
        os.makedirs(path, exist_ok=True)
        file_name = os.path.join(path, self.log_name + '.log')
        rotate_handler = logging.FileHandler(file_name, encoding='utf-8', mode='a')

        # handler对象 rotate_handler 的输出格式
        formatter = logging.Formatter(self.fmt)
        rotate_handler.setFormatter(formatter)

        return self.logger




